package com.akuhome.resource.service.need;

import com.akuhome.resource.DO.need.AccNeedDetail;
import com.akuhome.resource.DO.need.ClassroomNeed;
import com.akuhome.resource.DO.need.ClassroomNeedDetail;
import com.akuhome.resource.dao.NeedDao;
import com.akuhome.resource.enums.NeedStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class ClassroomNeedService implements NeedService<ClassroomNeed>{

    @Autowired
    private NeedDao needDao;

    @Override
    public void insertNeed(ClassroomNeed need) {
        needDao.insertClassroomNeed(need);
    }

    @Override
    public void insertNeedDetails(Map<String, Object> insertMap) {
        needDao.insertClassroomNeedDetails(insertMap);
    }

    @Override
    public ClassroomNeed findNeedByProjectUid(String projectUid) {
        return needDao.findClassroomNeedByProjectUid(projectUid);
    }

    @Override
    public void updateNeedStatusByProjectUid(String uid, NeedStatus needStatus) {
        needDao.updateClassroomNeedStatusByProjectUId(uid,needStatus);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateNeed(ClassroomNeed need) {
        needDao.updateClassroomNeed(need);
        HashMap<String,Object> updateMap = new HashMap<>();
        List<ClassroomNeedDetail> oldList = needDao.findClsNeedDetailByClsNeedId(need.getId());
        List<ClassroomNeedDetail> deleteList = new ArrayList<>(oldList);
        List<ClassroomNeedDetail> insertList = need.getDetailList();
        deleteList.removeAll(insertList);
        insertList.removeAll(oldList);
        updateMap.put("id",need.getId());
        updateMap.put("detailList",insertList);
        if(insertList.size()>0)
            needDao.updateClassroomNeedDetails(updateMap);
        if(deleteList.size()>0)
            needDao.deleteClassroomNeedDetails(deleteList);
    }

}
